package com.amkai.aes.webservice.binary.objecthandler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amkai.aes.aescommon.service.binary.job.InsurancePolicyCaseVerificationCommiterJob;
import com.amkai.aes.generatedbeans.insurance.InsurancePolicyCaseVerifiation;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class InsurancePolicyCaseVerificationCommiterHandler implements JobObjectHandler<InsurancePolicyCaseVerificationCommiterJob, InsurancePolicyCaseVerifiation, Boolean> {

	private static final Log logger = LogFactory.getLog(InsurancePolicyCaseVerificationCommiterHandler.class);

	@Override
	public Boolean process(InsurancePolicyCaseVerificationCommiterJob job, InsurancePolicyCaseVerifiation object) throws Exception {
		Boolean result = Boolean.TRUE;

		SessionManager sm = SiteManager.Start();
		try {
			sm.openSession();
			sm.openTransaction();

			if (object.getInNetworkBenefits() != null)
				sm.saveRAD(object.getInNetworkBenefits());
			
			if (object.getOutNetworkBenefits() != null)
				sm.saveRAD(object.getOutNetworkBenefits());
			
			if (object.getPhone() != null)
				sm.saveRAD(object.getPhone());
			
			if (object.getPreApprovalPhone() != null)
				sm.saveRAD(object.getPreApprovalPhone());

			sm.saveRAD(object);
			
			sm.commitTransaction();
		} catch (Exception e) {
			result = Boolean.FALSE;
			logger.error("", e);
			throw e;
		} finally {
			sm.closeSession();
		}
		return result;
	}

}
